﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MdsSaveLoad;
using System.Text.RegularExpressions;
using jtext_EpicsClient;
using JtextEpicsInterface;

namespace MdsplusDriver
{
    public class MdsDriver:IJEDriver
    {
        //use top in set
        //use \\top in get
        //this class lab is a template for making a driver for JEpicsServer
        //use only static functions
        private const string DName = "MdsDriver";  //indecate the driver's name
        private bool IsRunning;
        
        public event FlushRecordHandler OnFlush;    //rais a event the tell the record to flush if it's passive flush
        #region IJEDriver

        public bool StartDriver(Record_prop DesRecord)
        {
            return true;
        }

        public MdsDriver()
        {
            //consturctor
            IsRunning=false;
        }

        private int getShot(string strShot)
        {
            try
            {
                return Convert.ToInt32(strShot);
            }
            catch
            {
                //no a number, try epics pv
                try
                {
                    jtext_EpicsClient.JECLient myClient = new JECLient();
                    myClient.AddChannel(strShot);
                    int shotNo =Convert.ToInt32( myClient.RecordVAL[strShot].ToString());
                    myClient.DisposeAll();
                    return shotNo;
                }
                catch
                {
                    return 0;
                }
            }
            
        }

        public object GetVal(Record_prop DriverProp)    //this is a reload funtion ,couse different JEpicsRecord use the same driver
        {
            //this is a reload funtion ,couse different JEpicsRecord use the same driver
            //the parameter is used to determin which function to use
            
            
            if (IsRunning == false)
            {
                throw new SystemException("MdsDriver is not running");
            }
            string tree;
            string node;
            string strShot;
            int shotNo;
            string host;
            //host;tree;node;shot
            string strConfig = (string)DriverProp.Drv_path;
            string[] strConfigs = Regex.Split(strConfig, ";", RegexOptions.IgnoreCase);
            int configIndex = strConfig.IndexOf(DName) + 1;
            
            host= strConfigs[configIndex];
            Console.WriteLine("host: " + host);
            tree=strConfigs[configIndex + 1];
            Console.WriteLine("tree: " + tree);
            node = strConfigs[configIndex + 2];
            Console.WriteLine("node: " + node);
            strShot = strConfigs[configIndex + 3];
            Console.WriteLine("shot: " + strShot);
            shotNo = getShot(strShot);

            return MdsSaveLoad.MdsSaveLoad.MdsLoadU(tree, node, host, shotNo);
        }


        public object SetVal(Record_prop DriverProp, object value)
        {
            if (IsRunning == false)
            {
                throw new SystemException("MdsDriver is not running");
            }
            string tree;
            string node;
            string strShot;
            int shotNo;
            string host;
            //host;tree;node;shot
            string strConfig = (string)DriverProp.Drv_path;
            string[] strConfigs = Regex.Split(strConfig, ";", RegexOptions.IgnoreCase);
            int configIndex = strConfig.IndexOf(DName) + 1;

            host = strConfigs[configIndex];
            tree = strConfigs[configIndex + 1];
            node = strConfigs[configIndex + 2];
            strShot = strConfigs[configIndex + 3];
            //shotNo = getShot(strShot);
            
            if (DriverProp.DataClass == 0)
            {
                try
                {
                    double putDou = Convert.ToDouble(value);
                    MdsSaveLoad.MdsSaveLoad.MdsSave(putDou, tree, node, host, getShot(strShot));
                }
                catch
                {
                    long putLong = Convert.ToInt64(value);
                    MdsSaveLoad.MdsSaveLoad.MdsSave(putLong, tree, node, host, getShot(strShot));
                }
            }
            else
            {
                double[] putArray = (double[])value;
                MdsSaveLoad.MdsSaveLoad.MdsSave(putArray, tree, node, host, getShot(strShot));
            }
            return null ;
        }
        public bool IniHard(Record_prop DriverProp) //initialize the whole hardware
        {
            //this will accturely pass no parameter here
            //so if you need some config information use the driver name and load from a config file
            if (IsRunning == true)
            {
                return false;
            }
            IsRunning=true ;
            Console.WriteLine( "MdsDrier initialized");
            return true;  
        
        }
        public bool IniChannel(Record_prop DriverProp)  //initialize some part of the hardware
        {
            //you don't have to config or init the channel
            //since you1only have to channels that are init when IniHard,and they are fix need no config
            Console.WriteLine(DriverProp.Drv_path + "channel initialized");
            return true;
        }


        public void KillChannel(Record_prop DesRecord)
        {
 
        }//kill a channel not usually needed
        public void KillHard(Record_prop DesRecord)
        {
            IsRunning = false;
        }//kill a the whole driver


        public string DriverName 
        { 
            get
            {
                return DName;
            }
        }

        #endregion

    }
}
